package io.github.definitlyevil.ziminmod.listeners;

import de.slikey.effectlib.effect.AtomEffect;
import de.slikey.effectlib.effect.BleedEffect;
import de.slikey.effectlib.effect.CylinderEffect;
import io.github.definitlyevil.customitemframework.CIFItemInstance;
import io.github.definitlyevil.customitemframework.CustomItemFramework;
import io.github.definitlyevil.ziminmod.ZiMinMod;
import io.github.definitlyevil.ziminmod.cif.tools.ZiMinSword;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.SoundCategory;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ZiMinSwordListener implements Listener {

    private static final List<String> ATTACK_SOUNDS = new ArrayList<>();
    static {
        ATTACK_SOUNDS.add("zimin.ah-dah");
        ATTACK_SOUNDS.add("zimin.dah");
        ATTACK_SOUNDS.add("zimin.ya");
        // ATTACK_SOUNDS.add("zimin.ah.ah3");
        ATTACK_SOUNDS.add("zimin.zhennanren.zhennanren5");
        ATTACK_SOUNDS.add("zimin.shili.shili1");
        ATTACK_SOUNDS.add("zimin.shili.shili3");
        ATTACK_SOUNDS.add("zimin.shili.shili4");
    }

    private final Random random = new Random();

    private String randomAttackSound() {
        return ATTACK_SOUNDS.get(random.nextInt(ATTACK_SOUNDS.size()));
    }

    /**
     * 检查伤害
     * @param event
     */
    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onAttack(EntityDamageByEntityEvent event) {
        if(!LivingEntity.class.isAssignableFrom(event.getDamager().getClass())) return;
        LivingEntity living = (LivingEntity) event.getDamager();
        if(!checkHoldingZiMinSword(living)) return;
        Location location = living.getLocation();
        location.getWorld().playSound(location,
            randomAttackSound(),
            SoundCategory.PLAYERS,
            1.0f,
            1.0f + ((random.nextBoolean() ? 1f : -1f) * random.nextFloat()*0.1f)
        );

        BleedEffect bleed = new BleedEffect(ZiMinMod.getInstance().getEffectManager());
        bleed.setEntity(event.getEntity());
        bleed.duration = 10;
        bleed.start();

        AtomEffect atom = new AtomEffect(ZiMinMod.getInstance().getEffectManager());
        atom.setTargetLocation(event.getEntity().getLocation().add(0, 4, 0));
        atom.duration = 10;
        atom.start();
    }

    /**
     * 检查嗝儿屁
     * @param event
     */
    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onDeath(EntityDeathEvent event) {
        EntityDamageEvent cause = event.getEntity().getLastDamageCause();
        if(cause == null || !EntityDamageByEntityEvent.class.isAssignableFrom(cause.getClass())) return;
        EntityDamageByEntityEvent ev = (EntityDamageByEntityEvent) cause;
        if(Player.class.isAssignableFrom(ev.getDamager().getClass())) {
            // damaged by player
            if(checkHoldingZiMinSword(((LivingEntity) ev.getDamager()))) {
                CylinderEffect effect = new CylinderEffect(ZiMinMod.getInstance().getEffectManager());
                effect.setLocation(event.getEntity().getLocation());
                effect.radius = 2f;
                effect.height = 4f;
                effect.particle = Particle.DRIP_LAVA;
                effect.particles = 400;
                effect.duration = 20;
                effect.start();

                Location location = event.getEntity().getLocation();
                location.getWorld().playSound(location, "zimin.effect.attack", SoundCategory.AMBIENT, 1.0f,
                        1.0f + ((random.nextBoolean() ? 1f : -1f) * random.nextFloat() * 0.2f)
                );
                Bukkit.getScheduler().runTaskLater(ZiMinMod.getInstance(), () -> {
                    location.getWorld().playSound(location, "zimin.effect.swoosh", SoundCategory.AMBIENT, 1.0f,
                        1.0f + ((random.nextBoolean() ? 1f : -1f) * random.nextFloat() * 0.2f)
                    );
                }, 40L);

            }
        }
    }

    private static boolean checkHoldingZiMinSword(LivingEntity living) {
        if(living == null || living.getEquipment() == null) return false;
        ItemStack item = living.getEquipment().getItemInMainHand();
        CIFItemInstance instance = CustomItemFramework.getCIFDataFromItem(item);
        if(instance == null || !ZiMinSword.class.isAssignableFrom(instance.type.getClass())) return false;
        return true;
    }

}
